// 处理微博模型显示内容--对应的视图是首页的cell
//  StatusViewModel.swift
//  WeiboSwift
//
//  Created by Apple13 on 16/7/14.
//  Copyright © 2016年 yupeng. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {

    //转发数
    var retweetStr: String?
    
    
    //评论数
    var commentStr:String?
    
    
    //点赞数
    var unlikeStr:String?
    
    //‼️ 干啥用的
    var retweetContent: String?
    
    //认证类型等级图片
    var verifiedTypeImage: UIImage?   //认证类型①
    
    //微博来源
    var sourceContent:String? //  设置来源①
    
    
    //会员等级图片
    var mbrankImage:UIImage?
    
    //持有微博模型
    var status:WBStatus?
    
    init(status:WBStatus) {
        super.init()
        
        self.status = status
        
        retweetStr = handleCount(status.reposts_count, defaultTitle: "转发")
        commentStr = handleCount(status.comments_count, defaultTitle: "评论")
        unlikeStr =  handleCount(status.attitudes_count, defaultTitle: "点赞")
        
        //处理转发微博内容显示
        handleRetweetContent(status)
        
        //  处理认证类型等级图片
       handVerifiedTypeImage(status.user?.verified_type ?? 0)  //认证类型③

        
        //处理来源字符串
        handleSourceContent(status.source ?? "")
        
        //处理会员等级图片
        handleMbrankImage(status.user?.mbrank ?? 0)
        
    }
    
    //处理会员等级图片
    private func handleMbrankImage(mbrank:Int){
        
        if mbrank >= 1 && mbrank <= 6 {
            let image = UIImage(named:"common_icon_membership_level\(mbrank)")
            mbrankImage = image
        }

        
        
    }
    
    
    //处理认证类型图片
    private func handVerifiedTypeImage(verifiedType: Int){//认证类型②
        
        //返回
        switch verifiedType {
        case 0:
            verifiedTypeImage = UIImage(named: "avatar_vip")
        case 2,3,5://企业认证
            verifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
            
        case 220://微博达人
             verifiedTypeImage =  UIImage(named: "avatar_grassroot")
        default:
            verifiedTypeImage = nil
        }
        
    }
    
    //处理来源字符串
    private func handleSourceContent(source: String){
        
        guard let range1 = source.rangeOfString("\">"),let range2 = source.rangeOfString("</") else {
            return
        }
        
        //运行到此范围一定存在
        
        //开始的光标位置
        let startIndex = range1.endIndex
        //结束的光标位置
        let endIndex = range2.startIndex
        
        //截取字符串
        sourceContent = "来至 " + source.substringWithRange(startIndex..<endIndex)
        
    }
    
    
    
    
    
    
    
    
    
    //处理转发微博
    private func handleRetweetContent(status:WBStatus){
        
        if status.retweeted_status != nil {
            
            //获取转发用户的名字
            guard let  name = status.retweeted_status?.user?.screen_name,let text = status.retweeted_status?.text  else{
                 return
            }
            
            //运行到此,name和转发微博内容一定有值
            retweetContent = "@\(name):\(text)"
           
        }
        
        
    }
    
    
    //处理数字格式
    private func handleCount(count:Int,defaultTitle:String) -> String {
        
        if count > 0 {
            if count >= 10000{
                //进行格式化处理
                let result = Float(count/1000)/10
                var resultStr = "\(result)万"
                
                if resultStr.containsString(".0") {
                    resultStr = resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                    
                }
                return resultStr
            }else{
                return "\(count)"
            }
        }else{
            return defaultTitle
        }
        
    }
    
    
    
}
